package com.gang.core.pluginMgr;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;



public class PluginClassLoader extends URLClassLoader {
	private static List<JarURLConnection> cacheJarConn=new ArrayList<JarURLConnection>();
	public PluginClassLoader() {
		super(new URL[]{},findParentClassLoader());
	}
	public void addURLFile(URL jarURL){
		URLConnection jarConn;
		try {
			jarConn = jarURL.openConnection();
			if(jarConn instanceof JarURLConnection){
				jarConn.setUseCaches(true);
				((JarURLConnection)jarConn).getManifest();
				cacheJarConn.add((JarURLConnection)jarConn);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		addURL(jarURL);
	}
	public void unloadJar(){
		for (JarURLConnection url : cacheJarConn) {
			System.out.println("卸载Jar包");
			try {
				url.getJarFile().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			url=null;
		}
	}
	private static ClassLoader findParentClassLoader(){
		ClassLoader parent=PluginManager.class.getClassLoader();
		if(parent==null){
			parent=PluginClassLoader.class.getClassLoader();
		}
		if(parent==null){
			parent=ClassLoader.class.getClassLoader();
		}
		return parent;
	}
}
